package com.bird.component.lang.common.jackson

import com.bird.component.lang.util.log
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.convertValue
import com.fasterxml.jackson.module.kotlin.readValue

inline fun <reified T, reified V>  T?.toKtData(objectMapper: ObjectMapper): V? {
    return when {
        this == null -> null
        this is String -> {
            try {
                objectMapper.readValue<V>(this)
            } catch (e: Exception) {
              null
            }
        }
        else -> {
            try {
                objectMapper.convertValue<V>(this)
            } catch (e: Exception) {
                log.error("序列化出错：${e.message}", e)
                null
            }
        }
    }
}


